package com.erau.droidsatframework.core;


import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.BatteryManager;
import android.os.Binder;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;

import com.erau.droidsatframework.control.ControlService;
//import com.erau.droidsatframework.control.ControlAlgorithmService; (depreciated)

public class CoreService extends Service {
	
	//Services
	private final IBinder localBinder = new CoreBinder(); //For dealing with incoming binds	
	
	Intent controlIntent;
	Intent controlAlgoIntent;
	
	//Service Flags
	boolean controlStarted = false;
	boolean controlAlogrithmStarted = false;
	
	//Logging
	public DataAccessLayer dal;
	private static final String TAG = "Core Service";
	
	//METHOD: onBind
	//Android specific method that returns an IBinder which allows
	//the clients that are binding this service to interface.
	@Override
	public IBinder onBind(Intent arg0) {
		return localBinder; //Return the IBinder to the system to allow binding
	}//End onBind()
	
	//CLASS: CoreBinder
	//Class that contains the information for the CoreService... 
	//We have a method so that multiple things can bind into us...
	public class CoreBinder extends Binder {
		public CoreService getService() {
			return CoreService.this;
		}//End getService
	}//End CoreBinder
	
	@Override
	public void onCreate() {
		//Open up the Database logging connection...
		dal = new DataAccessLayer(this);
		dal.open();
		//dal.logGeneral(TAG, "Service Starting...");
		//dal.logGeneral(TAG, "Logging Succesfully Started");
		
		//Register the battery status recievers
		registerReceiver(batteryStatusReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
		//dal.logGeneral(TAG, "Battery Receiver Registered");
		
		//dal.logGeneral(TAG, "Service Succesfully Started");	
	}//End onCreate

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		return START_NOT_STICKY; //We can die
	}//End onStartCommand//
	
	@Override
	public void onDestroy(){
		if(controlStarted){
			stopAlgorthim();
		}
		Log.v(TAG, "Core Destroy Called");
		//Toast.makeText(this, "Core is dead... its just dead...", 2000).show();
		dal.close();
		
	}
	
	public void startAlgorithm(){
		if(controlStarted == false){
			controlIntent = new Intent(this, ControlService.class);
			if(startService(controlIntent) != null ){
				controlStarted = true;
			}
		}
	}
	
	public void stopAlgorthim(){
		if(controlStarted == true){
			stopService(controlIntent);
			controlStarted = false;
		}
	}
	
	public void stopRequest(){
		if(controlStarted){
			stopAlgorthim();
		}
		Log.v(TAG, "Core stop req");
		stopSelf();
	}
	
	
	private final BroadcastReceiver batteryStatusReceiver = new BroadcastReceiver(){
		@Override
		public void onReceive(Context c, Intent i) {
			Bundle batInfo = i.getExtras();
			int batLevel = batInfo.getInt(BatteryManager.EXTRA_LEVEL);
			int batVolt = batInfo.getInt(BatteryManager.EXTRA_VOLTAGE);
			int batTemp = batInfo.getInt(BatteryManager.EXTRA_TEMPERATURE);
			long timestamp = System.currentTimeMillis();
			dal.logPower(timestamp, batLevel, batVolt, batTemp);
		}	
	};//end batteryStatusReciever
	
	
	
}//End of file
	
	
	
	
	
	